package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.entity.User;
import com.example.demo.entity.User;
import com.example.demo.pojo.UserAddDTO;
import com.example.demo.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Resource
    UserService userService;

    @GetMapping("/list")
    public Result list(){
        List<User> userList = userService.list();
        return Result.success(userList);
    }

    @PostMapping("/changeUser")
    public Result changeUser(@RequestBody UserAddDTO userAddDTO){
        User user = userService.selectByName(userAddDTO.getName());
        user.setName(userAddDTO.getName());
        user.setPhone(userAddDTO.getPhone());
        user.setIntro(userAddDTO.getIntro());
        user.setEmail(userAddDTO.getEmail());
        if(userService.changeUser(user)){
            return Result.success();
        }
        else {
            return Result.error("更新失败!");
        }

    }
}
